package com.zhn.evsi.interceptor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhn.evsi.client.AllianceClient;
import com.zhn.evsi.client.CharacterClient;
import com.zhn.evsi.client.CharacterPublicClient;
import com.zhn.evsi.client.CorporationClient;
import com.zhn.evsi.common.RedisCache;
import com.zhn.evsi.domain.dto.evsi.alliance.EvsiAllianceInfoDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterBloodlineDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterPortraitDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterPublicInfoDTO;
import com.zhn.evsi.domain.dto.evsi.common.EvsiIconDTO;
import com.zhn.evsi.domain.dto.evsi.corporation.EvsiCorporationPublicInfoDTO;
import com.zhn.evsi.domain.entity.System.SysUser;
import com.zhn.evsi.mapper.EvsiCharacterMapper;
import com.zhn.evsi.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class CharacterInterceptor {

    @Resource
    private RedisCache redisCache;
    @Autowired
    private EvsiCharacterMapper characterMapper;
    @Autowired
    private AllianceClient allianceClient;
    @Autowired
    private CharacterPublicClient characterPublicClient;
    @Autowired
    private CorporationClient corporationClient;
    @Autowired
    private ISysUserService userService;


    public EvsiCharacterPublicInfoDTO getCharacters(String username) {
        if (username == null) {
            // 通过jwt获取邮箱
            username = "1085155177@qq.com";
        }

        EvsiCharacterPublicInfoDTO characterPublicInfoDTO = redisCache.getCacheObject("User:Character:Main:" + username);
        if (characterPublicInfoDTO == null) {
            Integer characters = userService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username)).getCharacterMain();
            characterPublicInfoDTO = characterPublicClient.publicInfo(characters);

            EvsiAllianceInfoDTO evsiAllianceInfoDTO = allianceClient.alliancesInfo(characterPublicInfoDTO.getAlliance_id());
            EvsiIconDTO allianceIconIconDTO = allianceClient.allianceIcon(characterPublicInfoDTO.getAlliance_id());
            EvsiCorporationPublicInfoDTO corporations = corporationClient.corporations(characterPublicInfoDTO.getCorporation_id());
            EvsiIconDTO cropIconDTO = corporationClient.corporationIcon(characterPublicInfoDTO.getCorporation_id());

            characterPublicInfoDTO.setCharacters(characters);
            characterPublicInfoDTO.setAlliance_name(evsiAllianceInfoDTO.getName());
            characterPublicInfoDTO.setAlliance_icon(allianceIconIconDTO.getPx64x64());
            characterPublicInfoDTO.setCorporation_name(corporations.getName());
            characterPublicInfoDTO.setCorporation_icon(cropIconDTO.getPx64x64());

            EvsiCharacterBloodlineDTO bloodline = characterMapper.bloodline(characterPublicInfoDTO.getBloodline_id());
            characterPublicInfoDTO.setBloodline_name(bloodline.getBloodlineName() + "/" + bloodline.getRaceName());
            redisCache.setCacheObject("User:Character:Main:" + username, characterPublicInfoDTO, 2, TimeUnit.HOURS);
        }
        return characterPublicInfoDTO;
    }
}
